package com.yiren.dbaa.module.project_edit.presenter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.SingleLightEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.cache.GradeStandardCache;
import com.yiren.backstage.db.dao.cache.SingleLightCache;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.project_edit.view.adapter.SelectParamAdapter;
import com.yiren.dbaa.module.project_edit.contract.SingleLightContract;
import com.yiren.dbaa.module.project_edit.contract.SingleLightEditContract;
import com.yiren.dbaa.module.project_edit.model.SingleLightEditModel;
import com.yiren.dbaa.module.project_edit.model.SingleLightModel;
import com.yiren.dbaa.util.DensityUtil;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author lwb
 * @date : 2022/8/24 17:33
 * @description: 单项灯光编辑P
 */
public class SingleLightEditPresenter implements SingleLightEditContract.Presenter {

    private Activity activity;
    private SingleLightContract.Model singleLightModel;
    private SingleLightEditContract.Model lightEditModel;
    private Gson gson;

    public SingleLightEditPresenter(Activity activity) {
        this.activity = activity;
        singleLightModel = new SingleLightModel(activity);
        lightEditModel = new SingleLightEditModel(activity);
        gson = new Gson();
    }

    @Override
    public List<SingleLightEntity> getAllSingleLightList() {
        return singleLightModel.getAllSingleLight();
    }

    @Override
    public List<GradeStandardEntity> getGradeStandardTemplate() {
        return lightEditModel.getGradeStandardTemplate();
    }

    private String selectString = "扣100分";

    @Override
    public void loadGradeStandardDialog(SingleLightEditContract.View v, int type, String title) {
        List<String> selectList = new ArrayList<>();
        List<GradeStandardEntity> gradeStandardList = lightEditModel.getGradeStandardTemplate();
        Dialog dialog = new AlertDialog.Builder(activity).create();
        dialog.show();
        WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = (DensityUtil.getScreenWidth(activity) * 7) / 10;
        params.height = (DensityUtil.getScreenHeight(activity) * 6) / 10;
        dialog.getWindow().setAttributes(params);
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_select_box);
        TextView tvTitle = (TextView) window.findViewById(R.id.tv_title);
        ListView lvList = (ListView) window.findViewById(R.id.lv_dialog_list);
        lvList.setDivider(null);// 去除listview的下划线
        tvTitle.setText(title);
        if (type == 2) {
            selectList.add("自定义");
            for (int i = 0; i < gradeStandardList.size(); i++) {
                selectList.add(gradeStandardList.get(i).getStandardContent() + "(扣" + gradeStandardList.get(i).getGrade() + "分)");
            }
        } else if (type == 3) {
            selectList.add("扣100分");
            selectList.add("扣10分");
            selectList.add("扣5分");
        }
        SelectParamAdapter adapter = new SelectParamAdapter(activity, selectList, selectString);
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener((parent, view, position, id) -> {
            switch (type) {
                case 3:  //扣分
                    int grade;
                    if (position == 0) {
                        grade = 100;
                    } else if (position == 1) {
                        grade = 10;
                    } else {
                        grade = 5;
                    }
                    v.showPoints(grade);
                    selectString = selectList.get(position);
                    break;
                case 2:
                    selectString = selectList.get(position);
                    if (selectString.contains("自定义")) {
                        v.showPointsContent("自定义", null, true);
                        v.showPoints(100);
                    } else {
                        v.showPointsContent(selectString, gradeStandardList.get(position - 1), false);
                        v.showPoints(gradeStandardList.get(position - 1).getGrade());
                    }
                    break;
            }
            dialog.dismiss();
        });
    }

    @Override
    public void saveSingleLight(SiteEntity site, String time, String lightContent, GradeStandardEntity gradeStandard, String gradeStandardContent, int points, HashMap<String, String> lightValue, SingleLightEntity singleLightEntity) {
        if (time.equals("") || lightContent.equals("")) {
            MessageBox.show(activity, "灯光操作完成时间或者灯光内容不能为空!");
            return;
        }
        SynExamBean synExamBean;
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
        if (userParamConfigBean.isPartner()) {
            synExamBean = gson.fromJson(site.getPartneModifyVal(), new TypeToken<SynExamBean>() {
            }.getType());
        } else {
            synExamBean = gson.fromJson(site.getCoachModifyVal(), new TypeToken<SynExamBean>() {
            }.getType());
        }
        if (synExamBean == null) {
            synExamBean = new SynExamBean();
        }
        if (synExamBean.getSingleLightMap() == null) synExamBean.setSingleLightMap(new HashMap<>());
        if (synExamBean.getGradeStandardMap() == null)
            synExamBean.setGradeStandardMap(new HashMap<>());

        boolean isGrade = false;
        if (gradeStandard == null) {
            //自定义扣分
            gradeStandard = new GradeStandardEntity();
            gradeStandard.setId(UUID.randomUUID().toString());
            gradeStandard.setStandardContent(gradeStandardContent);
            gradeStandard.setGrade(points);
            isGrade = true;
        } else {
            if (gradeStandard.getProjectId() == null) {
                if (!gradeStandard.getStandardContent().equals(gradeStandardContent) || gradeStandard.getGrade() != points)
                    isGrade = true;
                gradeStandard.setStandardContent(gradeStandardContent);
                gradeStandard.setGrade(points);
            }
        }
        if (isGrade)
            synExamBean.getGradeStandardMap().put(gradeStandard.getId(), new SynExamBean.ObjectClass(gradeStandard, 1));
        GradeStandardCache.getAllGradeStandard().put(gradeStandard.getId().hashCode(), gradeStandard);

        //修改
        boolean isModifyPut = false;
        if (singleLightEntity != null) {
            if (singleLightEntity.getWaitTime() != Integer.parseInt(time) || !StringUtils.equals(singleLightEntity.getContent(), lightContent)
                    || !StringUtils.equals(singleLightEntity.getStateValue(), new Gson().toJson(lightValue)) || !StringUtils.equals(singleLightEntity.getGradeStandardId(), gradeStandard.getId()))
                isModifyPut = true;
            singleLightEntity.setWaitTime(Integer.parseInt(time));
            singleLightEntity.setContent(lightContent);
            singleLightEntity.setStateValue(new Gson().toJson(lightValue));
            singleLightEntity.setGradeStandardId(gradeStandard.getId());
            SingleLightEntity singleLight_map = null;
            try {
                singleLight_map = (SingleLightEntity) singleLightEntity.clone();
                singleLight_map.setGradeStandardEntity(null);
                singleLight_map.setStateValueShow(null);
                singleLight_map.setGradeStandardId(gradeStandard.getId());
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            if (isModifyPut) {
                if (synExamBean.getSingleLightMap().containsKey(singleLightEntity.getId())) {
                    if (synExamBean.getSingleLightMap().get(singleLightEntity.getId()).getState() == 1) {
                        synExamBean.getSingleLightMap().put(singleLightEntity.getId(), new SynExamBean.ObjectClass(singleLight_map, 1));
                    } else {
                        synExamBean.getSingleLightMap().put(singleLightEntity.getId(), new SynExamBean.ObjectClass(singleLight_map, 2));
                    }
                } else {
                    synExamBean.getSingleLightMap().put(singleLightEntity.getId(), new SynExamBean.ObjectClass(singleLight_map, 2));
                }
            }
            singleLightEntity.setGradeStandardEntity(gradeStandard);
            SingleLightCache.getAllSingleLight().put(singleLightEntity.getId(), singleLightEntity);
        } else {
            //新增
            SingleLightEntity singleLight = new SingleLightEntity();
            singleLight.setId(UUID.randomUUID().toString());
            singleLight.setWaitTime(Integer.parseInt(time));
            singleLight.setContent(lightContent);
            singleLight.setStateValue(new Gson().toJson(lightValue));
            singleLight.setGradeStandardId(gradeStandard.getId());
            SingleLightEntity singleLight_map = null;
            try {
                singleLight_map = (SingleLightEntity) singleLight.clone();
                singleLight_map.setGradeStandardEntity(null);
                singleLight_map.setStateValueShow(null);
                singleLight_map.setGradeStandardId(gradeStandard.getId());
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            synExamBean.getSingleLightMap().put(singleLight.getId(), new SynExamBean.ObjectClass(singleLight_map, 1));
            singleLight.setGradeStandardEntity(gradeStandard);
            SingleLightCache.getAllSingleLight().put(singleLight.getId(), singleLight);
        }

        if (userParamConfigBean.isPartner()) {
            site.setPartneModifyVal(gson.toJson(synExamBean));
        } else {
            site.setCoachModifyVal(gson.toJson(synExamBean));
        }
        UserDbController.getInstance(false).update(site);
        MessageBox.show(activity, "保存成功");
        activity.finish();
    }


}
